package com.msh.automatic.android.api.controller.device;

import com.msh.automatic.android.api.entity.TaskLog;
import com.msh.automatic.android.api.mapper.TaskLogMapper;
import com.msh.automatic.android.api.util.RetUtil;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@Slf4j
@RestController
@RequestMapping("/d/task")
public class TaskController {

    @Resource
    TaskLogMapper taskLogMapper;

    @ApiOperation(value = "任务日志")
    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public Object log(@RequestBody TaskLog params) throws UnsupportedEncodingException {
        TaskLog taskLog = taskLogMapper.selectById(params.getId());
        if(taskLog == null){
            return RetUtil.error("任务不存在");
        }
        taskLog.setStatus(params.getStatus());
        taskLog.setMessage(params.getMessage());
        taskLogMapper.updateById(taskLog);
        return RetUtil.success(taskLog);
    }

    @ApiOperation(value = "任务信息")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public Object info(@RequestBody TaskLog params) throws UnsupportedEncodingException {
        TaskLog taskLog = taskLogMapper.selectById(params.getId());
        if(taskLog == null){
            return RetUtil.error("任务不存在");
        }
        return RetUtil.success(taskLog);
    }
}
